package instrarbre;

import exprarbre.ASAExpr;
import java.util.HashMap;
import javax.swing.JTextArea;

public class ASAIf extends ASAInstr {

    private ASAExpr expression;
    private ASAInstr instructionIf;
    private ASAInstr instructionElse;

    public ASAIf(ASAExpr expression, ASAInstr instructionIf) {
        this.expression = expression;
        this.instructionIf = instructionIf;
    }

    public ASAIf(ASAExpr expression, ASAInstr instructionIf, ASAInstr instructionElse) {
        this(expression, instructionIf);
        this.instructionElse = instructionElse;
    }

    @Override
    public void interpreter(HashMap<String, Integer> affectation) {
        if (expression.evalue(affectation) != 0) {
            instructionIf.interpreter(affectation);
        }
        else {
            if (instructionElse != null) {
                instructionElse.interpreter(affectation);
            }
        }
    }

    @Override
    protected String notationExplorateur(String prefixe) {
        String res = prefixe + "IF\n";
        String prefixeSuiv = prefixe + "  ";
        res += instructionIf.notationExplorateur(prefixeSuiv);
        if (instructionElse != null) {
            res += prefixe + "ELSE\n";
            res += instructionElse.notationExplorateur(prefixeSuiv);
        }
        return res;
    }

    @Override
    public void setView(JTextArea area) {
        instructionIf.setView(area);
        if (instructionElse != null) {
            instructionElse.setView(area);
        }
    }
}